﻿
using System.Collections.Generic;
using ConfiguracionFisica.Persistencia;

namespace ConfiguracionFisica.Dominio
{
    public class ConversorSerialMock : IConversorSerialDao
    {
        private static ConversorSerialMock singletonInstance;
        private ISet<ConversorSerial> conversoresSerial;

        public static ConversorSerialMock getInstance()
        {
            if (singletonInstance == null) singletonInstance = new ConversorSerialMock();
            return singletonInstance;
        }

        private ConversorSerialMock()
        {
            this.conversoresSerial = new HashSet<ConversorSerial>();
            List<Puerto> puertosDelConversor=new List<Puerto>();
            puertosDelConversor.Add(new PuertoEntradaSalida(1000));
            puertosDelConversor.Add(new PuertoEntradaSalida(1001));
            puertosDelConversor.Add(new PuertoEntradaSalida(1002));
            puertosDelConversor.Add(new PuertoEntradaSalida(1003));

            this.conversoresSerial.Add(new ConversorSerial("Subsuelo","192.168.1.10", puertosDelConversor,true));
            this.conversoresSerial.Add(new ConversorSerial("IngresoPrincipal","192.168.1.125", puertosDelConversor,true));
            this.conversoresSerial.Add(new ConversorSerial("Subsuelo", "192.168.1.120", puertosDelConversor, true));
        }

        public ISet<ConversorSerial> getTodosConversoresSerial()
        {
            return conversoresSerial;
        }

        public void eliminarConversorSerial(string ipConversorSerial)
        {
            ConversorSerial conversorAEliminar = getConversorPorIp(ipConversorSerial);
            if (conversorAEliminar != null) conversoresSerial.Remove(conversorAEliminar);
        }

        public ConversorSerial guardarOModificarConversorSerial(ConversorSerial conversorSerialAGuardar)
        {
            ConversorSerial conversorSerialExistente = null;

            operation<ConversorSerial, bool> buscarSiExiste = unConversorSerial => unConversorSerial.Equals(conversorSerialAGuardar);
            foreach (ConversorSerial unConversorSerial in conversoresSerial)
            {
                if (buscarSiExiste(unConversorSerial))
                {
                    conversorSerialExistente = unConversorSerial;
                    break;
                }
            }

            if (conversorSerialExistente != null)
            {
                conversoresSerial.Remove(conversorSerialExistente);
                conversoresSerial.Add(conversorSerialAGuardar);
            }

            conversoresSerial.Add(conversorSerialAGuardar);
            return conversorSerialExistente;
        }

        public ConversorSerial getConversorPorIp(string ipConversorSerial)
        {
            foreach (ConversorSerial unConversorSerial in this.conversoresSerial)
            {
                if (unConversorSerial.DireccionIp.Equals(ipConversorSerial)) return unConversorSerial;
            }
            return null;
        }
    }
}
